Building Renewal Copenhagen
Introduction
The script in this R Markdown document is a part of a spatial project exploring the topic of building renewal in Copenhagen.
The script in this document does the following:
- Creates a map showing the buildings in Copenhagen which have undergone building renewal.
- Transforms and combines two CSV files showing the number of buildings older than 1899 per district.
- Combines this data with spatial data showing the different districts of Copenhagen. Makes an accompanying map.
Data used
The data used has been created by Copenhagen Municipality.
- Københavns Kommunes Statistikbank. “KKBYG1.” Accessed August 11, 2023. https://kk.statistikbank.dk/statbank5a/SelectVarVal/Define.asp?MainTable=KKBYG1&PLanguage=0&PXSId=0&wsid=cflist.
- Open Data DK. “Bydele.” Accessed August 11, 2023, https://www.opendata.dk/city-of-copenhagen/bydele.
- Open Data DK. “Byfornyelse.” Accessed August 11, 2023. https://www.opendata.dk/city-of-copenhagen/byfornyelse#resource-byfornyelse.
- Open Data DK. “Udsatte byområder.” Accessed August 11, 2023. https://www.opendata.dk/city-of-copenhagen/udsatte-byomrader.
License: Attribution 4.0 International (CC BY 4.0): https://creativecommons.org/licenses/by/4.0/
Loading Packages
Firstly, I need to load the packages for this spatial project. The following packages have been used:
- tidyverse (1.3.0)
- sf (1.0-6) for processing the spatial data
- rgdal (1.5-28) for reading spatial data
- leaflet (2.1.0) for creating the maps.
## Error : 'format_warning' is not an exported object from 'namespace:cli'
## ── Attaching packages ─────────────────────────────────────── tidyverse 1.3.0 ──
## ✓ ggplot2 3.3.2 ✓ purrr 0.3.4
## ✓ tibble 3.0.4 ✓ dplyr 1.0.2
## ✓ tidyr 1.1.2 ✓ stringr 1.4.0
## ✓ readr 1.4.0 ✓ forcats 0.5.0
## ── Conflicts ────────────────────────────────────────── tidyverse_conflicts() ──
## x dplyr::filter() masks stats::filter()
## x dplyr::lag() masks stats::lag()
## Linking to GEOS 3.9.1, GDAL 3.4.0, PROJ 8.1.1; sf_use_s2() is TRUE
## Loading required package: sp
## Please note that rgdal will be retired by the end of 2023,
## plan transition to sf/stars/terra functions using GDAL and PROJ
## at your earliest convenience.
##
## rgdal: version: 1.5-28, (SVN revision 1158)
## Geospatial Data Abstraction Library extensions to R successfully loaded
## Loaded GDAL runtime: GDAL 3.2.1, released 2020/12/29
## Path to GDAL shared files: /Library/Frameworks/R.framework/Versions/4.0/Resources/library/rgdal/gdal
## GDAL binary built with GEOS: TRUE
## Loaded PROJ runtime: Rel. 7.2.1, January 1st, 2021, [PJ_VERSION: 721]
## Path to PROJ shared files: /Library/Frameworks/R.framework/Versions/4.0/Resources/library/rgdal/proj
## PROJ CDN enabled: FALSE
## Linking to sp version:1.4-6
## To mute warnings of possible GDAL/OSR exportToProj4() degradation,
## use options("rgdal_show_exportToProj4_warnings"="none") before loading sp or rgdal.
## Overwritten PROJ_LIB was /Library/Frameworks/R.framework/Versions/4.0/Resources/library/rgdal/proj
Building Renewal and Vulnerable Areas
Reading the spatial data
I’m going to read the spatial data downloaded from Open Data DK by using the readLines function. (resource consulted: https://rstudio.github.io/leaflet/json.html )
# loading the spatial file showing renewed buildings
building_renewal <- readLines("data/byfornyelse_kk.json") %>%
paste(collapse = "\n")## Warning in readLines("data/byfornyelse_kk.json"): incomplete final line found on
## 'data/byfornyelse_kk.json'
# loading the spatial file showing vulnerable areas
vulnerable_areas <- readLines("data/f_udsatte_byomraader.json") %>%
paste(collapse = "\n")## Warning in readLines("data/f_udsatte_byomraader.json"): incomplete final line
## found on 'data/f_udsatte_byomraader.json'
Creating the Leaflet map
Then I’m create the map using Leaflet.
leaflet() %>%
addTiles() %>% # adding basic tiles
setView(lng = 12.52840564444299, lat = 55.658326021508024, zoom = 11) %>% #setting coordinates
addGeoJSON(building_renewal, weight = 2, fillOpacity = 1) %>% # adding geojson feature
addGeoJSON(vulnerable_areas, color = "#ED2828", weight = 1) # adding second geojson featureBuilding Renewal and Building Ages
Loading data
District map
The first file I will need to load is the geojson file from Open Data DK showing the different districts of Copenhagen. I am reading it using rgdal this time since I am going to combine it with non spatial data. By using rgdal I also get access to more functions of customization. (resource consulted: https://rstudio.github.io/leaflet/json.html )
## OGR data source with driver: GeoJSON
## Source: "/Users/rie/Desktop/building_renewal_CPH/data/bydel.json", layer: "bydel"
## with 10 features
## It has 5 fields
Here is the spatial data showing the city districts on its own:
## Warning in readLines("data/bydel.json"): incomplete final line found on 'data/
## bydel.json'
Data wrangling
Selecting the year 2023
The CSV files contains information on the number of buildings registered from 1991 to 2023. I have chosen to use 2023 since it appears like there was a lot of building registration done in 2017, and I want the data to be as complete as possible.
I am going the use the filter function to only include that year.
Selecting the rows showing the districts
The CSV file contains a lot of additional information besides the information on the ten districts. I am going to select the rows where the name start with “bydel” (district)
(tutorial consulted: https://stackoverflow.com/questions/28860069/regular-expressions-regex-and-dplyrfilter )
Matching the name formating
I need to make sure that the names in the data frame is the same as in the geojson so that I can combine them later on.
I am changing the name of the “OMRKK” column in the dataframe to match the “navn” (name) column in the geojson
Additionally, This means that I have to remove the “Bydel -” part from the names in the data frame.
Combining the two data frames
At the moment, I have two seperate data frames and I want to combine them using the function inner_join().
bfr1899 <- inner_join(bfr1850, from1850to1899,
by = c("navn", "ENHED", "TID")) # choosing the columns that matchI’m also adding the “INDHOLD” (contents) column together so that it shows the number of buildings before 1899.
(Tutorial consulted: https://dplyr.tidyverse.org/reference/mutate.html)
Combining the two datasets
The last step before creating the map is to combine the two datasets using the merge function. (tutorial consulted: https://stackoverflow.com/questions/46695816/merge-csv-and-json-file-in-leaflet-map-strange-mistake)
Creating the Leaflet map
I’m going to create a palette so that the different districts are shaded according to the continuous data.
Now I can create the second map:
leaflet(bfr1899_map) %>%
addTiles() %>%
setView(lng = 12.52840564444299, lat = 55.658326021508024, zoom = 10) %>%
addPolygons(weight = 1,
fillColor = ~palette(bfr1899_map$combined), # using palette from before
fillOpacity = 0.75,
label = ~paste0(navn, ": ", formatC(combined, big.mark = ","))) %>%
addLegend("bottomright", pal = palette, values = ~bfr1899_map$combined,
opacity = 1,
title = "Number of buildings older than 1899 by district") %>%
addGeoJSON(building_renewal, weight = 2, fillOpacity = 1, color = "orange")References
Data Used
- Københavns Kommunes Statistikbank. “KKBYG1.” Accessed August 8, 2023. https://kk.statistikbank.dk/statbank5a/SelectVarVal/Define.asp?MainTable=KKBYG1&PLanguage=0&PXSId=0&wsid=cflist.
- Open Data DK. “Bydele.” Accessed August 8, 2023, https://www.opendata.dk/city-of-copenhagen/bydele.
- Open Data DK. “Byfornyelse.” Accessed August 8, 2023. https://www.opendata.dk/city-of-copenhagen/byfornyelse#resource-byfornyelse.
- Open Data DK. “Udsatte byområder.” Accessed August 8, 2023. https://www.opendata.dk/city-of-copenhagen/udsatte-byomrader.
License for data used
Attribution 4.0 International (CC BY 4.0): https://creativecommons.org/licenses/by/4.0/
Tutorials and Internet Resources Consulted
- dplyr. “Create, modify, and delete columns.” Accessed August 10. https://dplyr.tidyverse.org/reference/mutate.html
- Leaflet for R. “Working with GeoJSON & TopoJSON.” Accessed July 31, 2023. https://rstudio.github.io/leaflet/json.html.
- StackOverflow. “Regular expressions (RegEx) and dplyr::filter().” Accessed August 10. 2023. https://stackoverflow.com/questions/28860069/regular-expressions-regex-and-dplyrfilter
- StackOverflow. “Merge csv and json file in Leaflet map - strange mistake.” Accessed August 10. https://stackoverflow.com/questions/46695816/merge-csv-and-json-file-in-leaflet-map-strange-mistake